﻿

using System;
using Newtonsoft.Json.Linq;
using log4net;


namespace CommonLib
{

    public class RefinPropertyData
    {
        private static readonly log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);

        public RefinPropertyData(string json)
         : this(JObject.Parse(json))
        {
        }

        public RefinPropertyData( JObject obj, bool soldFlag): this(obj)
        {
            isSold = soldFlag;            
        }

        public RefinPropertyData(JObject obj)
        {
            try
            {
                this.is_short_sale = JsonClassHelper.ReadBoolean(JsonClassHelper.GetJToken<JValue>(obj, "is_short_sale"));
                this.dom = JsonClassHelper.ReadInteger(JsonClassHelper.GetJToken<JValue>(obj, "dom"));
                this.original_price = JsonClassHelper.ReadInteger(JsonClassHelper.GetJToken<JValue>(obj, "original_price"));
                this.hoa_dues = JsonClassHelper.ReadInteger(JsonClassHelper.GetJToken<JValue>(obj, "hoa_dues"));
                this.visibility_profile = JsonClassHelper.ReadString(JsonClassHelper.GetJToken<JValue>(obj, "visibility_profile"));
                this.mls_status = JsonClassHelper.ReadInteger(JsonClassHelper.GetJToken<JValue>(obj, "mls_status"));
                this.type = JsonClassHelper.ReadString(JsonClassHelper.GetJToken<JValue>(obj, "type"));
                //this.date = JsonClassHelper.ReadString(JsonClassHelper.GetJToken<JValue>(obj, "date"));
                JObject dateJobj = obj["date"] as JObject;
                if (dateJobj != null)
                {
                    this.date = dateJobj.ToString();
                }
                
                this.sqft = JsonClassHelper.ReadInteger(JsonClassHelper.GetJToken<JValue>(obj, "sqft"));
                this.available_photos = JsonClassHelper.ReadString(JsonClassHelper.GetJToken<JValue>(obj, "available_photos"));
                this.search_status = JsonClassHelper.ReadInteger(JsonClassHelper.GetJToken<JValue>(obj, "search_status"));
                this.id = JsonClassHelper.ReadInteger(JsonClassHelper.GetJToken<JValue>(obj, "id"));
                this.parcel = JsonClassHelper.ReadStronglyTypedObject<RedfinParcel>(JsonClassHelper.GetJToken<JObject>(obj, "parcel"));
                this.is_new = JsonClassHelper.ReadBoolean(JsonClassHelper.GetJToken<JValue>(obj, "is_new"));
                this.price_display = JsonClassHelper.ReadString(JsonClassHelper.GetJToken<JValue>(obj, "price_display"));
                this.sale_listing_id = JsonClassHelper.ReadInteger(JsonClassHelper.GetJToken<JValue>(obj, "sale_listing_id"));
                this.URL = JsonClassHelper.ReadString(JsonClassHelper.GetJToken<JValue>(obj, "URL"));
                this.baths = JsonClassHelper.ReadFloat(JsonClassHelper.GetJToken<JValue>(obj, "baths"));
                this.address_data = JsonClassHelper.ReadStronglyTypedObject<RedfinAddressData>(JsonClassHelper.GetJToken<JObject>(obj, "address_data"));
                this.year_built = JsonClassHelper.ReadInteger(JsonClassHelper.GetJToken<JValue>(obj, "year_built"));
                this.access_level = JsonClassHelper.ReadInteger(JsonClassHelper.GetJToken<JValue>(obj, "access_level"));
                this.last_price_reduction_date = JsonClassHelper.ReadString(JsonClassHelper.GetJToken<JValue>(obj, "last_price_reduction_date"));
                this.listing_type = JsonClassHelper.ReadInteger(JsonClassHelper.GetJToken<JValue>(obj, "listing_type"));
                this.neighborhood = JsonClassHelper.ReadString(JsonClassHelper.GetJToken<JValue>(obj, "neighborhood"));
                this.property = JsonClassHelper.ReadStronglyTypedObject<RedfinProperty>(JsonClassHelper.GetJToken<JObject>(obj, "property"));
                this.service_region_type = JsonClassHelper.ReadInteger(JsonClassHelper.GetJToken<JValue>(obj, "service_region_type"));
                this.service_policy = JsonClassHelper.ReadInteger(JsonClassHelper.GetJToken<JValue>(obj, "service_policy"));
                this.open_house_time_end = JsonClassHelper.ReadLong(JsonClassHelper.GetJToken<JValue>(obj, "open_house_time_end"));
                this.lotsize = JsonClassHelper.ReadInteger(JsonClassHelper.GetJToken<JValue>(obj, "lotsize"));
                this.display_order = JsonClassHelper.ReadInteger(JsonClassHelper.GetJToken<JValue>(obj, "display_order"));
                this.datasource_id = JsonClassHelper.ReadInteger(JsonClassHelper.GetJToken<JValue>(obj, "datasource_id"));
                this.securePhotoNum = JsonClassHelper.ReadInteger(JsonClassHelper.GetJToken<JValue>(obj, "securePhotoNum"));
                this.mlsid = JsonClassHelper.ReadString(JsonClassHelper.GetJToken<JValue>(obj, "mlsid"));
                this.beds = JsonClassHelper.ReadInteger(JsonClassHelper.GetJToken<JValue>(obj, "beds"));
                this.price = JsonClassHelper.ReadInteger(JsonClassHelper.GetJToken<JValue>(obj, "price"));
                this.listing_broker_name = JsonClassHelper.ReadString(JsonClassHelper.GetJToken<JValue>(obj, "listing_broker_name"));
                this.market_id = JsonClassHelper.ReadInteger(JsonClassHelper.GetJToken<JValue>(obj, "market_id"));
                this.fixer = JsonClassHelper.ReadBoolean(JsonClassHelper.GetJToken<JValue>(obj, "fixer"));
                this.open_house_time_display = JsonClassHelper.ReadString(JsonClassHelper.GetJToken<JValue>(obj, "open_house_time_display"));
                this.listingBroker = JsonClassHelper.ReadString(JsonClassHelper.GetJToken<JValue>(obj, "listingBroker"));
                this.sharedNotesVisibility = JsonClassHelper.ReadInteger(JsonClassHelper.GetJToken<JValue>(obj, "sharedNotesVisibility"));
                this.listing_added = JsonClassHelper.ReadLong(JsonClassHelper.GetJToken<JValue>(obj, "listing_added"));
                this.open_house_time_start = JsonClassHelper.ReadLong(JsonClassHelper.GetJToken<JValue>(obj, "open_house_time_start"));
                this.property_type_raw = JsonClassHelper.ReadInteger(JsonClassHelper.GetJToken<JValue>(obj, "property_type_raw"));
                this.display_type = JsonClassHelper.ReadInteger(JsonClassHelper.GetJToken<JValue>(obj, "display_type"));
            }
            catch (Exception e)
            {
                log.ErrorFormat("Error in JSON Parsing : {0} ", e.StackTrace);                
            }
        }


        public readonly bool isSold ;
        public readonly bool is_short_sale;
        public readonly int dom;
        public readonly int original_price;
        public readonly int hoa_dues;
        public readonly string visibility_profile;
        public readonly int mls_status;
        public readonly string type;
        public readonly string date;
        public readonly int sqft;
        public readonly string available_photos;
        public readonly int search_status;
        public readonly int id;
        public readonly RedfinParcel parcel;
        public readonly bool is_new;
        public readonly string price_display;
        public readonly int sale_listing_id;
        public readonly string URL;
        public readonly double baths;
        public readonly RedfinAddressData address_data;
        public readonly int year_built;
        public readonly int access_level;
        public readonly string last_price_reduction_date;
        public readonly int listing_type;
        public readonly string neighborhood;
        public readonly RedfinProperty property;
        public readonly int service_region_type;
        public readonly int service_policy;
        public readonly long open_house_time_end;
        public readonly int lotsize;
        public readonly int display_order;
        public readonly int datasource_id;
        public readonly int securePhotoNum;
        public readonly string mlsid;
        public readonly int beds;
        public readonly int price;
        public readonly string listing_broker_name;
        public readonly int market_id;
        public readonly bool fixer;
        public readonly string open_house_time_display;
        public readonly string listingBroker;
        public readonly int sharedNotesVisibility;
        public readonly long listing_added;
        public readonly long open_house_time_start;
        public readonly int property_type_raw;
        public readonly int display_type;
    }
}
